package com.nf.practice.downloadFile;

import cn.hutool.http.HttpUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.io.FileReader;
import java.util.*;
import java.util.stream.Collectors;

// 多线程分批次下载
// 根据指定条件下载影像件
public class AccordingCsvFormatDownloadFsFile {

    private static final int THREAD_POOL_SIZE = 15; // 线程池大小
    private static final int BUFFER_SIZE = 4096; // 缓冲区大小
    private static final int BATCH_SIZE = 200;

//    private static final List<String> idNoDirs = Arrays.asList("B","B01","B02");
//    private static final List<String> loanContractDirs = Arrays.asList("AA05", "AB05", "AC02", "AE03", "AF01", "AG02", "AD02", "AH01", "AI02", "U04", "U11", "K");
//    private static final List<String> guaranteeContractDirs = Arrays.asList("S", "S03", "AE04", "AA07", "U15");

    private static final List<String> idNoDirs = Arrays.asList("B","B01","B02");
    private static final List<String> loanContractDirs = Arrays.asList("K","O01");
    private static final List<String> guaranteeContractDirs = Arrays.asList("W", "P01", "S03");

    private static final List<String> targetDir = Arrays.asList(
            "B",
            "B01",
            "B02",
            "K",
            "O01",
            "W",
            "P01",
            "S03"
    );

    private static final List<String> jpgDir = Arrays.asList(
            "B",
            "B01",
            "B02"
    );

    private static final List<String> pdfDir = Arrays.asList(
            "K",
            "O01",
            "W",
            "P01",
            "S03"
    );

    // 目录英转中
    private static final Map<String, String> dirsEngToCh = new HashMap<String, String>() {
        {
            put("AA05", "新网借款合同");
            put("AB05", "苏宁额度借款合同");
            put("AC02", "华兴借款合同");
            put("AE03", "振兴借款合同");
            put("AF01", "中关村个人借款合同");
            put("AG02", "三湘个人借款合同");
            put("AD02", "借款合同");
            put("AH01", "借款合同");
            put("AI02", "华通借款合同");
            put("U04",  "一押借款/抵押合同");
            put("U11",  "借款合同");
            put("K",    "合同");
            put("AE12", "债转相关文件");
            put("AF03", "债转文件");
        }
    };

    // 1、根据单号下载影像
    @Test
    public void accordingAppNoDownloadImg2() {
        Long begin = System.currentTimeMillis();
        // 获取列表
        List<PicData2> picDataList = new ArrayList<>();
        String preDic = "C:/fsProdFile/";
        List<PicExportDetail2> dataList = new ArrayList<>();
        try {
            // 方法一读取CSV文件，该方法中FileReader 类是用于读取字符文件的，但它默认使用平台默认的字符编码
            FileReader reader = new FileReader("E:\\fs.csv");
            CSVParser parser = new CSVParser(reader, CSVFormat.DEFAULT);
            // 遍历CSV文件的每一行并获取单元格数据
            for (CSVRecord record : parser) {
                if ("app_no".equals(record.get(0))) continue;
                PicData2 picData2 = new PicData2();
                for (int i = 0; i < record.size(); i++) {
                    if (i == 0) {
                        picData2.setAppNo(record.get(i));
                    } else if (i == 1) {
                        picData2.setSubclassSort(record.get(i));
                    } else if (i == 2) {
                        picData2.setUrl(record.get(i));
                    }
                }
                picDataList.add(picData2);
            }
            // 关闭流
            parser.close();
            reader.close();
        } catch (Exception e) {
            System.out.println("异常了。。。。");
        }

        for (PicData2 picData : picDataList) {
            // 文件目录
            String subclassSort = picData.getSubclassSort();
            if (!targetDir.contains(subclassSort)) {
                continue;
            }

            int idNoNum = 0;
            int loanContractNum = 0;
            int guaranteeContractNum = 0;
            PicExportDetail2 data = new PicExportDetail2();

            // 文件地址
            String fileUrl = picData.getUrl();
            String appNo = picData.getAppNo();
            // 文件名
            String fileName = fileUrl;
            int lastIndex = fileName.lastIndexOf('/');
            if (lastIndex != -1) {
                // 截取最后一个'/'之后的所有字符
                fileName = fileName.substring(lastIndex + 1);
                if (jpgDir.contains(subclassSort) && (!fileName.contains(".jpg") || !fileName.contains(".png"))) {
                    fileName = fileName.concat(".jpg");
                } else if (pdfDir.contains(subclassSort) && !fileName.contains(".pdf")) {
                    fileName = fileName.concat(".pdf");
                }
            }
//                            // 目录中文名
//                        String chDir = dirsEngToCh.get(subclassSort);
//                            // 创建目录
//                    String directoryPath = "C:/fsFile/" + appNo + "/" + chDir + "/";

            String directoryPath = preDic + appNo + "/";

            File directory = new File(directoryPath);
            // 如果目录不存在，则创建它
            if (!directory.exists()) {
                directory.mkdirs();
            }
            // 创建文件对象，使用fileName作为文件名
            File file = new File(directory, fileName);
            // 使用Hutool的HttpUtil工具类下载影像件文件,文件保存的关键步骤
            try {
                HttpUtil.downloadFile(fileUrl, file);
            } catch (Exception e) {
                System.out.println(appNo + "-" + fileUrl + "-" + "下载失败!");
                // 下载异常了重新下载其他文件
                continue;
            }
            System.out.println(appNo + "-" + fileUrl + "-" + "下载成功!");
            if (idNoDirs.contains(subclassSort)) {
                idNoNum++;
            } else if (loanContractDirs.contains(subclassSort)) {
                loanContractNum++;
            } else if (guaranteeContractDirs.contains(subclassSort)) {
                guaranteeContractNum++;
            }
            data.setAppNo(appNo)
                    .setIdNoNum(idNoNum)
                    .setLoanContractNum(loanContractNum)
                    .setGuaranteeContractNum(guaranteeContractNum);
            dataList.add(data);
        }
        Map<String, PicExportDetail2> groupedData = dataList.stream()
                .collect(Collectors.groupingBy(
                        PicExportDetail2::getAppNo, // 按照 appNo 分组
                        Collectors.collectingAndThen(
                                Collectors.reducing((d1, d2) -> d1.merge(d2)), // 使用自定义的merge方法进行归约
                                Optional::get // 从Optional中获取结果
                        )
                ));

        // 输出结果
        List<PicExportDetail2> finalDataList = new ArrayList<>();
        groupedData.forEach((appNo, data) -> {
            finalDataList.add(data);
        });
        File directory = new File(preDic);
        // 如果目录不存在，则创建它
        if (!directory.exists()) {
            directory.mkdirs();
        }
        ExcelWriter writer = ExcelUtil.getWriter(new File(preDic + "fsFinalFile.xlsx"));
        if(!CollectionUtils.isEmpty(finalDataList)){
            writer.setSheet(0).write(finalDataList);
            writer.flush();
        }

        System.out.println("下载结束,计算耗时：" + (System.currentTimeMillis() - begin) + "ms");

    }

}
